package com.example.art.chapter3.one.one_four

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.View

/**
 * Created by 舍长 on 2019/4/8
 * describe:自定义View
 */
class VelocityDemo : View {

    constructor(context: Context?) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)


    @SuppressLint("Recycle")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        //速度追踪
        val velocity = VelocityTracker.obtain()
        velocity.addMovement(event)
        when (event.action) {
            //手指在屏幕上移动
            MotionEvent.ACTION_MOVE -> {
                //获取当前的滑动速度
                velocity.computeCurrentVelocity(1000)
                val xV = velocity.xVelocity
                val yY = velocity.yVelocity
                Log.d("hello", "xV：$xV")
                Log.d("hello", "yV：$yY")
            }
        }
        return super.onTouchEvent(event)
    }
}